// TODO 生成文件
import path from "path";
import fs from "fs";
import chalk from "chalk";
import inquirer from "inquirer";
import dedent from "dedent";
import ora from "ora";


const answer = await inquirer.prompt([
  {
    type: "input",
    name: "inputtedName",
    message: "请输入可组合函数名称（use 开头的小驼峰，就像这样：useDemo）:",
    validate: (inputtedName) => {
      const existingHooks = fs
        .readdirSync("./packages/core")
        .filter((i) => i.startsWith("use"));
      if (existingHooks.includes(inputtedName)) {
        return chalk.redBright("已存在，请重新编辑");
      }
      if (!inputtedName.startsWith("use")) {
        return chalk.redBright("应以 use 开头");
      }
      if (!/^use[A-Z][a-z]*$/.test(inputtedName)) {
        return chalk.redBright("不满足命名格式");
      }
      return true;
    },
  },
  {
    type: "list",
    name: "category",
    message: "请选择所属目录/类别",
    choices: ["core"],
  },
]);

const { inputtedName, category } = answer;
// const mdTemplate = ''
const mdTemplate = dedent(`
  # ${inputtedName}

  __DESCRIPTION

  ## Usage
  \`\`\`vue
  __USAGE_SLOT
  \`\`\`

  ## Demo
  __DEMO_SLOT
`);

const spinner = ora({color: "yellow", text: '正在创建...'}).start()
createHooks();

function createHooks() {
  setTimeout(() => {
    fs.mkdirSync(`packages/${category}/${inputtedName}`);
    fs.writeFileSync(`packages/${category}/${inputtedName}/index.ts`, "");
    fs.mkdirSync(`docs/examples/${inputtedName}`);
    fs.writeFileSync(`docs/examples/${inputtedName}/demo.vue`, "");
    fs.writeFileSync(`docs/examples/${inputtedName}/index.md`, mdTemplate);
    spinner.text = "创建完成";
    spinner.stop()
  }, 1800);
}
